package cn.billy.org.service.impl;


import cn.billy.basic.query.BaseQuery;
import cn.billy.basic.service.impl.BaseServiceImpl;
import cn.billy.basic.utils.PageList;
import cn.billy.org.domain.Department;
import cn.billy.org.mapper.DepartmentMapper;
import cn.billy.org.query.DepartmentQuery;
import cn.billy.org.service.IDepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
/**
* @Description: IDepartmentService
* @Author: Billy
* @Version: 1.0
* @Date:  2022/6/21 21:21
*/
@Service
@Transactional(readOnly = true, propagation = Propagation.SUPPORTS)
public class DepartmentServiceImpl extends BaseServiceImpl<Department> implements IDepartmentService {


    @Autowired
    private DepartmentMapper departmentMapper;


    /*
    * @Title: save
    * @Description: 新增
    * @Author: Billy
    * @Version: 1.0
    * @Date:  2022/6/21 21:23
    * @Parameters:
    * @Return
    */
    @Override
    @Transactional
    public void save(Department department) {
        departmentMapper.add(department);
        String dirPath = "";
        if (department.getDepartment().getId() == null){
            dirPath = "/" + department.getId();
        } else {
            Department dept = departmentMapper.getById(department.getDepartment().getId());
            dirPath = dept.getDirPath() + "/" + department.getId();

        }
        department.setDirPath(dirPath);
        departmentMapper.update(department);

    }

    /*
    * @Title: update
    * @Description: 修改
    * @Author: Billy
    * @Version: 1.0
    * @Date:  2022/6/21 21:23
    * @Parameters:
    * @Return
    */
    @Override
    @Transactional
    public void update(Department department) {
        String dirPath = "";
        if (department.getDepartment().getId() == null){
            dirPath = "/" + department.getId();
        } else {
            Department dept = departmentMapper.getById(department.getDepartment().getId());
            dirPath = dept.getDirPath() + "/" + department.getId();


        }
        department.setDirPath(dirPath);
        departmentMapper.update(department);
    }

    /*
    * @Title: queryData
    * @Description: 分页+高级查询
    * @Author: Billy
    * @Version: 1.0
    * @Date:  2022/6/21 21:24
    * @Parameters:
    * @Return
    */



    /*
    * @Title: departmentTree
    * @Description: 无限级数
    * @Author: Billy
    * @Version: 1.0
    * @Date:  2022/6/26 11:26
    * @Parameters:
    * @Return
    */
    @Override
    public List<Department> departmentTree() {
        List<Department> departments = departmentMapper.loadAll();
        List<Department> depts = new ArrayList<>();
        for(Department depart : departments){
            if (depart.getParentId() == null){
                depts.add(depart);
            } else {
                for (Department child : departments){
                    if (child.getId() == depart.getParentId()){
                        child.getChildren().add(depart);
                        break;
                    }
                }
            }
        }

        return depts;
    }
}
